package com.example.piece.ui.fragment

import android.graphics.Color
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.example.base.broadcast.Event
import com.example.base.broadcast.EventBusUtil
import com.example.base.ext.dip2px
import com.example.base.ext.getViewModel
import com.example.base.ext.navigationTo
import com.example.base.tools.JsonTool
import com.example.base.ui.fragment.BaseFragment
import com.example.base.ui.view.DividerItemDecoration
import com.example.base.ui.view.DividerItemDecoration.HORIZONTAL_LIST
import com.example.base.ui.view.drawable.getDimensionPixelSize
import com.example.base.ui.view.recyclerview.SpaceItemDecoration
import com.example.base.vm.onError
import com.example.piece.App
import com.example.piece.BR
import com.example.piece.R
import com.example.piece.constant.Key
import com.example.piece.constant.Key.EventKey.EVENT_UNREAD_NUM
import com.example.piece.constant.RoutePath
import com.example.piece.databinding.FragmentMessageBinding
import com.example.piece.ui.adapter.SystemMsgAdapter
import com.example.piece.vm.UserViewModel
import com.gyf.immersionbar.ktx.immersionBar

/**
 * 消息
 */
class MessageFragment : BaseFragment<FragmentMessageBinding>() {
    private val userViewModel: UserViewModel by lazy {
        getViewModel<UserViewModel>()
    }

    override fun isRegisterEventBus(): Boolean {
        return true
    }

    var page = 0;
    private val systemMsgAdapter: SystemMsgAdapter by lazy {
        val adapter = SystemMsgAdapter()
        mBinding.rv.adapter = adapter
        adapter
    }

    companion object {
        fun newInstance(): Fragment {
            val fragment = MessageFragment()
            return fragment
        }
    }


    override fun getContentRes(): Int = R.layout.fragment_message

    override fun initData(view: View, savedInstanceState: Bundle?) {
        super.initData(view, savedInstanceState)
        mBinding.fragment = this

        systemMsgAdapter.loadMoreModule.setOnLoadMoreListener {
            getMgs()

        }
        mBinding.srl.setOnRefreshListener {
            page = 1
            systemMsgAdapter.loadMoreModule.isEnableLoadMore = true
            systemMsgAdapter.loadMoreModule.isAutoLoadMore = true
            //  getMgs()
        }
        systemMsgAdapter.setOnItemClickListener(OnItemClickListener { adapter, view, position ->
            navigationTo(
                RoutePath.MAIN_SYSTEM_MSG_DETAIL_ACTIVITY, mutableMapOf(
                    Key.DataKey.H5_TITLE to "系统消息",
                    Key.DataKey.MSG_CONTENT to JsonTool.object2Json(adapter.data[position])
                )
            )
        })
        context?.let {
            mBinding.rv.addItemDecoration(DividerItemDecoration(context,
                HORIZONTAL_LIST,
                it.dip2px(2f).toInt(),
                Color.parseColor("#F9F9F9")))
        }

        if (!App.USER_TOKEN.isNullOrEmpty()) {
            getUnreadMsg()
        }
    }

    private fun getMgs() {
        if (!App.USER_TOKEN.isNullOrEmpty()) {
            getSystemMsg()

            getUnreadMsg()
        }
    }

    //获取未读消息个数
    private fun getUnreadMsg() {
        userViewModel.userMessageUnread(null) {//未读信息个数
            if (it.code == 200) {
                mBinding.setVariable(BR.msgTotal, it.data)
                EventBusUtil.sendEvent(Event<String>(EVENT_UNREAD_NUM,
                    (it.data.attentionMum.toLong() + it.data.commentNum.toLong() + it.data.systemNum.toLong() + it.data.likeNum.toLong()).toString()))

            }
        } onError {

        }
    }

    //获取系统消息
    private fun getSystemMsg() {
        userViewModel.userMessageSystem(null, page.toString()) {
            if (it.code == 200) {
                mBinding.srl.isRefreshing = false
                page++
                if (it.data.isNullOrEmpty()) {
                    systemMsgAdapter.loadMoreModule.isEnableLoadMore = false
                    systemMsgAdapter.loadMoreModule.isAutoLoadMore = false
                    return@userMessageSystem
                }
                if (page == 2) {
                    systemMsgAdapter.setList(it.data)
                } else {
                    systemMsgAdapter.addData(it.data)

                }
            }
        } onError {

        }
    }
    /**
     * type=0 赞 type=1 评论 type=2 关注
     */
    fun gotoMsgList(type: String) {
        var title=""
        when(type){
            "0"->{
                title="赞.踩"
            }
            "1"->{
                title="评论"
            }
            "2"->{
                title="关注"
            }
        }

        navigationTo(
            RoutePath.MAIN_SYSTEM_MSG_LIST_ACTIVITY, mutableMapOf(
                Key.DataKey.H5_TITLE to title,
                Key.DataKey.MSG_TYPE to type)
            )

    }

    override fun receiveEvent(event: Event<Any>?) {
        super.receiveEvent(event)
        when (event?.code) {
            Key.EventKey.EVENT_LOGIN -> {//登录成功
                getMgs()
            }

        }
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)

        if (hidden) {

        } else {
            immersionBar {
                fitsSystemWindows(true, com.example.base.R.color.white)
                statusBarDarkFont(true)
                //  statusBarAlpha(0.1f)
            }
            getMgs()
        }
    }

}